import { ref } from 'vue'

// 菜单状态管理
const todayMenus = ref({
  早餐: [
    { id: 1, name: '小米粥', type: '粥类', price: 3, description: '营养丰富的小米粥' },
    { id: 2, name: '包子', type: '主食', price: 2, description: '新鲜蒸制的包子' },
    { id: 3, name: '豆浆', type: '饮品', price: 2, description: '现磨豆浆' },
    { id: 4, name: '油条', type: '主食', price: 1.5, description: '香脆油条' },
    { id: 5, name: '煎饼果子', type: '主食', price: 4, description: '传统煎饼果子' }
  ],
  午餐: [
    { id: 6, name: '番茄炒蛋', type: '主菜', price: 12, description: '经典家常菜' },
    { id: 7, name: '青菜豆腐汤', type: '汤类', price: 8, description: '清淡营养汤品' },
    { id: 8, name: '米饭', type: '主食', price: 2, description: '香软白米饭' },
    { id: 9, name: '红烧肉', type: '主菜', price: 18, description: '肥而不腻的红烧肉' },
    { id: 10, name: '宫保鸡丁', type: '主菜', price: 15, description: '川菜经典' },
    { id: 11, name: '蒜蓉西兰花', type: '素菜', price: 10, description: '健康绿色蔬菜' }
  ],
  晚餐: [
    { id: 12, name: '清炒时蔬', type: '素菜', price: 10, description: '新鲜时令蔬菜' },
    { id: 13, name: '紫菜蛋花汤', type: '汤类', price: 6, description: '营养丰富的蛋花汤' },
    { id: 14, name: '馒头', type: '主食', price: 1, description: '松软馒头' },
    { id: 15, name: '糖醋里脊', type: '主菜', price: 15, description: '酸甜可口的里脊肉' },
    { id: 16, name: '冬瓜排骨汤', type: '汤类', price: 12, description: '清热解暑的排骨汤' }
  ]
})

// 未来菜单数据（模拟数据库）
const futureMenus = ref({})

// 获取今日菜单
export function getTodayMenus() {
  return todayMenus.value
}

// 更新今日菜单
export function updateTodayMenus(newMenus) {
  todayMenus.value = { ...newMenus }
}

// 更新特定餐次的菜单
export function updateCategoryMenus(category, menus) {
  todayMenus.value[category] = [...menus]
}

// 复制历史菜单到今天
export function copyHistoryToToday(historyMenus) {
  const newMenus = { 早餐: [], 午餐: [], 晚餐: [] }
  
  // 按餐次分组历史菜单
  historyMenus.forEach(menu => {
    if (newMenus[menu.category]) {
      newMenus[menu.category].push({
        id: Date.now() + Math.random(), // 生成新的ID
        name: menu.name,
        type: menu.type,
        price: menu.price,
        description: `${menu.name} - 来自历史菜单`
      })
    }
  })
  
  // 更新今日菜单
  todayMenus.value = newMenus
  return newMenus
}

// 复制单条菜品到指定日期
export function copySingleMenuToDate(menu, targetDate) {
  // 确保目标日期的菜单存在
  if (!futureMenus.value[targetDate]) {
    futureMenus.value[targetDate] = { 早餐: [], 午餐: [], 晚餐: [] }
  }
  
  // 创建新的菜品对象
  const newMenu = {
    id: Date.now() + Math.random(),
    name: menu.name,
    type: menu.type,
    price: menu.price,
    description: `${menu.name} - 来自${menu.date}的历史菜单`,
    category: menu.category,
    sourceDate: menu.date,
    targetDate: targetDate
  }
  
  // 添加到目标日期的对应餐次
  if (futureMenus.value[targetDate][menu.category]) {
    futureMenus.value[targetDate][menu.category].push(newMenu)
  }
  
  return newMenu
}

// 获取指定日期的菜单
export function getMenuByDate(date) {
  if (date === new Date().toISOString().split('T')[0]) {
    return todayMenus.value
  }
  return futureMenus.value[date] || { 早餐: [], 午餐: [], 晚餐: [] }
}

// 获取所有未来菜单数据
export function getAllFutureMenus() {
  return futureMenus.value
} 